package com.xbongbong.paas.controller.mobile;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.applist.pojo.vo.ListAppVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.through.pojo.dto.ResultThroughDTO;
import com.xbongbong.pro.statistic.through.pojo.vo.IndexThroughDTO;
import com.xbongbong.pro.weblist.pojo.vo.IndexCommunivateVo;
import com.xbongbong.saas.service.SaasListService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author  yuqian
 * @version v1.0
 * @date 2019/5/14 17:14
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/indexThrough")
public class MobileIndexThroughController {
    @Resource
    private SaasListService saasListService;

    /**
     * 手机端 指标穿透
     * @param resultThroughDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/through", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customChartResultMobileThrough(@RequestBody @Valid ResultThroughDTO resultThroughDTO, BindingResult br){
        XbbResponse<ListAppVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        }else {
            try{
                ListAppVO listAppVO = saasListService.customIndexMobileThrough(resultThroughDTO);
                response = new XbbResponse<>(listAppVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 手机端 自定义指标穿透
     * @param resultThroughDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/customThrough", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String mobileCustomThrough(@RequestBody @Valid ResultThroughDTO resultThroughDTO, BindingResult br){
        XbbResponse<IndexThroughDTO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        }else {
            try{
                IndexThroughDTO indexThroughDTO = saasListService.customIndexThrough(resultThroughDTO);
                response = new XbbResponse<>(indexThroughDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    /**
     * 手机端 跟进记录指标穿透
     * @param resultThroughDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/customerCommunicate/through", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String  customerCommunicateThrough(@RequestBody @Valid ResultThroughDTO resultThroughDTO, BindingResult br){
        XbbResponse<IndexCommunivateVo> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                IndexCommunivateVo indexCommunivateVo = saasListService.customerCommunicateIndexThrough(resultThroughDTO);
                response = new XbbResponse<>(indexCommunivateVo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
